package com.ruoyi.wcf.service.impl;

import java.util.Date;
import java.util.List;

import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.mall.domain.Region;
import com.ruoyi.mall.service.IRegionService;
import com.ruoyi.system.domain.SysUserRole;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.mapper.SysUserRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.wcf.mapper.WcfWareHouseMapper;
import com.ruoyi.wcf.domain.WcfWareHouse;
import com.ruoyi.wcf.service.IWcfWareHouseService;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

/**
 * 仓库管理Service业务层处理
 *
 * @author zhipeng
 * @date 2022-04-06
 */
@Service
public class WcfWareHouseServiceImpl implements IWcfWareHouseService
{
    @Autowired
    private WcfWareHouseMapper wcfWareHouseMapper;
    @Autowired
    private SysUserMapper userMapper;
    @Autowired
    private SysUserRoleMapper userRoleMapper;
    @Autowired
    private IRegionService regionService;

    /**
     * 查询仓库管理
     *
     * @param houseId 仓库管理主键
     * @return 仓库管理
     */
    @Override
    public WcfWareHouse selectWcfWareHouseByHouseId(Long houseId)
    {
        return wcfWareHouseMapper.selectWcfWareHouseByHouseId(houseId);
    }

    /**
     * 查询仓库管理列表
     *
     * @param wcfWareHouse 仓库管理
     * @return 仓库管理
     */
    @Override
    public List<WcfWareHouse> selectWcfWareHouseList(WcfWareHouse wcfWareHouse)
    {
        return wcfWareHouseMapper.selectWcfWareHouseList(wcfWareHouse);
    }

    /**
     * 新增仓库管理
     *
     * @param wcfWareHouse 仓库管理
     * @return 结果
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insertWcfWareHouse(WcfWareHouse wcfWareHouse)
    {
        //省份id
        Long provinceId = wcfWareHouse.getProvinceId();
        //仓库账号
        String houseAccount = wcfWareHouse.getHouseAccount();
        //仓库密码
        String housePassword = wcfWareHouse.getHousePassword();

        //新增用户管理对象
        SysUser sysUser = new SysUser();
        sysUser.setCreateBy(SecurityUtils.getUsername());
        sysUser.setUserName(houseAccount);
        sysUser.setNickName(houseAccount);
        sysUser.setPassword(SecurityUtils.encryptPassword(housePassword));
        userMapper.insertUser(sysUser);

        //用户角色关联
        SysUserRole userRole = new SysUserRole();
        userRole.setUserId(sysUser.getUserId());
        userRole.setRoleId(5L);//5-分仓管理员
        userRoleMapper.insertUserRole(userRole);

        Region region = regionService.selectRegionById(provinceId);
        //新增仓库管理
        wcfWareHouse.setHouseName(region.getShortname());
        wcfWareHouse.setHousrUserId(sysUser.getUserId());
        wcfWareHouse.setCreateAt(new Date());
        int i = wcfWareHouseMapper.insertWcfWareHouse(wcfWareHouse);

        if (i == 0){
            //手动回滚
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return i;
    }

    /**
     * 修改仓库管理
     *
     * @param wcfWareHouse 仓库管理
     * @return 结果
     */
    @Override
    public int updateWcfWareHouse(WcfWareHouse wcfWareHouse)
    {
        return wcfWareHouseMapper.updateWcfWareHouse(wcfWareHouse);
    }

    /**
     * 批量删除仓库管理
     *
     * @param houseIds 需要删除的仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteWcfWareHouseByHouseIds(Long[] houseIds)
    {
        return wcfWareHouseMapper.deleteWcfWareHouseByHouseIds(houseIds);
    }

    /**
     * 删除仓库管理信息
     *
     * @param houseId 仓库管理主键
     * @return 结果
     */
    @Override
    public int deleteWcfWareHouseByHouseId(Long houseId)
    {
        return wcfWareHouseMapper.deleteWcfWareHouseByHouseId(houseId);
    }
}
